package com.vinoxm.rss.dao;

import com.vinoxm.rss.model.RssDetail;
import com.vinoxm.rss.model.RssLink;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RssLinkDao extends RssDetailDao {

    @Insert("<script>" +
            "INSERT INTO rss_link " +
            "(pid, title, href) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.pid},#{item.title},#{item.href})" +
            "</foreach>" +
            " ON DUPLICATE KEY UPDATE title=VALUES(title),href=VALUES(href)" +
            "</script>")
    void insertMany(@Param("list") List<? extends RssDetail> list);

    @Insert("<script>" +
            "INSERT INTO rss_link " +
            "(pid, title, href) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{pid},#{item.title},#{item.href})" +
            "</foreach>" +
            " ON DUPLICATE KEY UPDATE title=VALUES(title),href=VALUES(href)" +
            "</script>")
    void insertManyWithPid(@Param("list") List<? extends RssDetail> list,@Param("pid") long pid);

    @Select("SELECT id,pid,title,href FROM rss_link WHERE pid=#{pid} ORDER BY id")
    List<RssLink> selectByPid(long pid);

    @Insert("INSERT INTO rss_link (pid, title, href) VALUES (#{pid},#{title},#{href}) ON DUPLICATE KEY UPDATE title=VALUES(title),href=VALUES(href)")
    int insertOne(RssLink rssLink);

    @Update("UPDATE rss_link SET title=#{title},href=#{href} WHERE id=#{id}")
    int updateOne(RssLink rssLink);

    @Delete("DELETE FROM rss_link WHERE id=#{id}")
    int deleteOne(long id);

    @Delete("DELETE FROM rss_link WHERE pid=#{pid}")
    int deleteManyByPid(long pid);

    @Delete("<script>" +
            "DELETE FROM rss_link WHERE pid IN (" +
            "<foreach collection='pids' item='item' separator=','>" +
            "#{item}" +
            "</foreach>" +
            ")" +
            "</script>")
    int deleteManyByPids(List<Long> pids);

    @Select("SELECT pid FROM rss_link WHERE id=#{id}")
    Long selectPidById(long pid);
}
